package com.mylibrary.util;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yinsujun  Created at 2016/7/26 15:56
 */
public class Tools {
    private static Toast TOAST = null;
    @SuppressLint("ShowToast")
    public static void showTextToast(Context context, String msg) {
        if (TOAST == null) {
            TOAST = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            TOAST.setText(msg);
        }
        TOAST.show();
    }
    /**
     * 版本号
     *
     * @param con
     * @return
     */
    public static int getAppVersionCode(Context con) {
        try {
            PackageInfo pkgInfo = con.getPackageManager().getPackageInfo(
                    con.getPackageName(), 0);
            if (pkgInfo != null) {
                return pkgInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 版本名称
     *
     * @param con
     * @return
     */
    public static String getAppVersionName(Context con) {
        try {
            PackageInfo pkgInfo = con.getPackageManager().getPackageInfo(
                    con.getPackageName(), 0);
            if (pkgInfo != null) {
                return pkgInfo.versionName;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }



    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    public static void hideInputMethod(Activity activity) {
        if (null == activity) {
            return;
        }
        if (null != activity.getCurrentFocus() && null != activity.getCurrentFocus().getWindowToken()) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    //OOM出现 解决办法
    public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
        Bitmap bitmap = null;
        try {
            bitmap = Bitmap.createBitmap(width, height, config);
        } catch (OutOfMemoryError e) {
            while (bitmap == null) {
                System.gc();
                System.runFinalization();
                bitmap = createBitmap(width, height, config);
            }
        }
        return bitmap;
    }

	/*
	*方法注释为加载短声音播放，主要是为了游戏中添加声音
	*/
	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void audioDeal(){
        SoundPool sp;
		if((android.os.Build.VERSION.SDK_INT) >= 21) {//大于21出现需要处理的加载声音的逻辑
			SoundPool.Builder spb = new SoundPool.Builder();
			AudioAttributes.Builder aatrub=new AudioAttributes.Builder();
			aatrub.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
			spb.setAudioAttributes(aatrub.build());
			spb.setMaxStreams(4);
            sp= spb.build();
        }else{
			//兼容以前的低版本，soundpool已经过时
			sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		}
	}

}
